package com.bs.redis.controller;

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bingshao
 * @date 2023/3/15
 **/
@RestController
@RequestMapping("/geo")
public class GeoController {

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @PostMapping("/geoAdd")
    public void geoAdd() {
        Map<String, Point> map = new HashMap<>(4);
        map.put("天安门", new Point(116.403963, 39.915119));
        map.put("故宫", new Point(116.403414, 39.924091));
        map.put("长城", new Point(116.024067, 40.362639));
        redisTemplate.opsForGeo().add("city", map);
    }

    @GetMapping("/position")
    public void position() {
        // 查看天安门和长城的经纬度
        List<Point> position = redisTemplate.opsForGeo().position("city", "天安门", "长城");
        position.forEach(System.out::println);
        // Point [x=116.403963, y=39.915120]
        // Point [x=116.024066, y=40.362640]
    }

    @GetMapping("/distance")
    public void distance() {
        // 查询天安门和故宫的距离，单位km
        Distance distance = redisTemplate.opsForGeo().distance("city", "天安门", "故宫", RedisGeoCommands.DistanceUnit.KILOMETERS);
        System.out.println(distance.getValue() + distance.getUnit()); // 0.9988km
    }

    @GetMapping("/radius")
    public void radius() {
        //返回10条
        RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending().limit(10);
        //半径100公里内
        Distance distance = new Distance(100, Metrics.KILOMETERS);
        Circle circle = new Circle(new Point(115.000000, 40.000000), distance);
        // 指定经纬度查询100公里范围的地点从近到远
        GeoResults<RedisGeoCommands.GeoLocation<String>> geoResults1 = redisTemplate.opsForGeo().radius("city", circle, args);
        geoResults1.forEach(geoLocationGeoResult -> {
            System.out.println("geoResults1========");
            System.out.println(geoLocationGeoResult.getContent().getName());
            System.out.println(geoLocationGeoResult.getDistance().getValue());
            System.out.println(geoLocationGeoResult.getDistance().getMetric());
            //geoResults1========
            //长城
            //95.9151
            //KILOMETERS
        });

        // 指定存入的地点即天安门查询100公里范围的地点从近到远 ，结果包含本身
        GeoResults<RedisGeoCommands.GeoLocation<String>> geoResults2 = redisTemplate.opsForGeo().radius("city", "天安门", distance, args);
        geoResults2.forEach(geoLocationGeoResult -> {
            System.out.println("geoResults2========");
            System.out.println(geoLocationGeoResult.getContent().getName());
            System.out.println(geoLocationGeoResult.getDistance().getValue());
            System.out.println(geoLocationGeoResult.getDistance().getMetric());
            //geoResults2========
            //天安门
            //0.0
            //KILOMETERS
            //geoResults2========
            //故宫
            //0.9988
            //KILOMETERS
            //geoResults2========
            //长城
            //59.339
            //KILOMETERS

        });

    }

    @GetMapping("/geoHash")
    public void geoHash() {
        List<String> hash = redisTemplate.opsForGeo().hash("city", "天安门");
        System.out.println(hash.get(0)); //wx4g0f6f2v0
    }


}
